package com.ebelter.temperaturegun.ui.activity;


import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ebelter.btcomlib.CommonLib;
import com.ebelter.btcomlib.models.beans.UpdateVersionBean;
import com.ebelter.btcomlib.models.eventbus.CommonEventBus;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.utils.AppUtils;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.StringUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.btcomlib.views.dialogs.TextTipDialog;
import com.ebelter.temperaturegun.R;
import com.ebelter.temperaturegun.model.http.TGNetUtils;
import com.ebelter.temperaturegun.model.http.TemUpdateService;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class AboutActivity extends TBaseActivity {

    public static final String TAG = "AboutActivity";

    private ImageView top_iv;
    private TextView top_tv1;
    private TextView ab_version_tv;

    private String haveDownedStr;

    private RelativeLayout about_djgx_rl;

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        haveDownedStr=getString(R.string.yxz_d);
        top_iv=findViewById(R.id.top_iv);
        top_tv1=findViewById(R.id.top_tv1);
        about_djgx_rl=findViewById(R.id.about_djgx_rl);
        ab_version_tv=findViewById(R.id.ab_version_tv);
        top_tv1.setText(R.string.str_About);
        ViewUtils.displayView(top_tv1);
        top_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        about_djgx_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showUpdateDialog(AboutActivity.this,mUpdateVersionBean);
            }
        });
        ab_version_tv.setText(AppUtils.getVerName(this));
    }



    private TextTipDialog dialog;
    private void showUpdateDialog(Activity activity, UpdateVersionBean mUpdateVersionBean) {
        if (activity==null||activity.isFinishing()||activity.isDestroyed()||mUpdateVersionBean==null){
            return;
        }

        if (dialog!=null){
            dialog.cancel();
            dialog=null;
        }

        dialog=new TextTipDialog(activity, StringUtils.getResStr(R.string.New_version_is_available_touch_to_update));
        dialog.isPingBiBack=true;
        dialog.show();
        // dialog.setNativebtVisibility(View.GONE);
        dialog.setPositive_btBg(R.drawable.sp_s0176ff_r6);
        dialog.setListener(new TextTipDialog.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {
                dialogView.cancel();
            }
            @Override
            public void ok(Dialog dialogView) {
                dialogView.cancel();
                update();
            }
        });
    }

    //更新按钮
    private void update() {
        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }
        if (mUpdateVersionBean==null){
            return;
        }

        Intent updateIntent=new Intent(this,TemUpdateService.class);
        updateIntent.putExtra(TemUpdateService.DownloadSer,mUpdateVersionBean);
        startService(updateIntent);

    }


    @Override
    protected void initData() {
        getNetVersion();
    }

    @Override
    protected int setContentViewId() {
        return R.layout.activity_about;
    }

    UpdateVersionBean mUpdateVersionBean;
    public void getNetVersion() {
        if (NetUtils.available()){
            final int verCode= AppUtils.getVersionCode(this);//获取当前版本号
            LogUtils.i(TAG,"-------verCode---" + verCode);
            TGNetUtils.getInstance().getUpdateVersionJson(this, new HttpResponse<UpdateVersionBean>() {
                @Override
                public void result(boolean isRequestSuccess, String requestDes, UpdateVersionBean updateVersionBean, String s) {
                    LogUtils.i(TAG,"s---"+s);
                    if (isRequestSuccess&&updateVersionBean!=null){

                        LogUtils.i(TAG,"服务器上的json--"+updateVersionBean.toString());
                        if (NumUtils.string2Int(updateVersionBean.getVerCode())>verCode){

                            mUpdateVersionBean=updateVersionBean;
                            String s1= CommonLib.getString(R.string.djgx_)+" : "+updateVersionBean.getFileName();
                            ViewUtils.setTextViewStr(ab_version_tv,s1);

                        }else {
                            ViewUtils.setTextViewStr(ab_version_tv,AppUtils.getVerName(AboutActivity.this)+CommonLib.getString(R.string.Latest));
                        }
                    }
                }
            });
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(CommonEventBus event) {
        if (!event.whichReceive.contains(TAG)) {
            return;
        }
        LogUtils.i(TAG, "AboutActivity--onEventMainThread接收到CommonEventBus信息--" + event.toString());

        float p = (float) event.getContent();

        ToastUtil.show(haveDownedStr+" : "+ NumUtils.numBaoLiuWei1(p*100)+"%");


    }
}
